package com.ruoyi.help.service.impl;

import com.ruoyi.help.domain.Appointment;
import com.ruoyi.help.domain.Transfer;
import com.ruoyi.help.mapper.AppointmentMapper;
import com.ruoyi.help.mapper.TransferMapper;
import com.ruoyi.help.service.ITransferService;
import com.ruoyi.help.utils.EnterpriseWeChatRobot;
import com.ruoyi.help.utils.MailUtils;
import com.ruoyi.help.vo.TransferVo;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 问题移交Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@Service
public class TransferServiceImpl implements ITransferService {
    @Autowired
    private TransferMapper transferMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    /**
     * 查询问题移交
     *
     * @param id 问题移交主键
     * @return 问题移交
     */
    @Override
    public Transfer selectTransferById(Long id) {
        return transferMapper.selectTransferById(id);
    }

    /**
     * 查询问题移交列表
     *
     * @param transfer 问题移交
     * @return 问题移交
     */
    @Override
    public List<Transfer> selectTransferList(Transfer transfer) {
        return transferMapper.selectTransferList(transfer);
    }

    /**
     * 新增问题移交
     */
    @Override
    @Transactional
    public int insertTransfer(TransferVo transferVo) {
        Map<String, String> map = teacherMapper.findAll().stream().filter(teacher -> teacher.getName().equals(transferVo.getTeacherName())).collect(Collectors.toMap(Teacher::getName, Teacher::getEmail));
        if (transferVo.getChecked()) {
            EnterpriseWeChatRobot.sendMan(transferVo.getTakeName());
            MailUtils.sendMail(
                    map.get(transferVo.getTeacherName()),
                    "任务移交提醒",
                    "<h3>     " + transferVo.getTakeName() +
                            "  老师麻烦您处理帮扶任务<a href='http://localhost/helpJob/helpTransfer'>点击我</a>前去查看</h3>");
        }
        Appointment appointment = appointmentMapper.selectAppointmentById(transferVo.getId().toString());
        Transfer transfer = new Transfer();

        transfer.setTransferName(appointment.getTeacherName());
        transfer.setTakeName(transferVo.getTakeName());
        transfer.setTransferName(transferVo.getTeacherName());
        transfer.setTransferTime(new Date());
        transfer.setTransfeNote(transferVo.getTransfeNote());
        transfer.setHelpName(transferVo.getHelpName());

        appointment.setHelpType(3L);
        appointmentMapper.updateAppointment(appointment);

        return transferMapper.insertTransfer(transfer);
    }

    /**
     * 修改问题移交
     *
     * @param transfer 问题移交
     * @return 结果
     */
    @Override
    public int updateTransfer(Transfer transfer) {
        return transferMapper.updateTransfer(transfer);
    }

    /**
     * 批量删除问题移交
     *
     * @param ids 需要删除的问题移交主键
     * @return 结果
     */
    @Override
    public int deleteTransferByIds(Long[] ids) {
        return transferMapper.deleteTransferByIds(ids);
    }

    /**
     * 删除问题移交信息
     *
     * @param id 问题移交主键
     * @return 结果
     */
    @Override
    public int deleteTransferById(Long id) {
        return transferMapper.deleteTransferById(id);
    }
}
